<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
	<title>IP 数据类型 | ElasticSearch 7.7 权威指南中文版</title>
	<meta name="keywords" content="ElasticSearch 权威指南中文版, elasticsearch 7, es7, 实时数据分析，实时数据检索" />
    <meta name="description" content="ElasticSearch 权威指南中文版, elasticsearch 7, es7, 实时数据分析，实时数据检索" />
    <!-- Give IE8 a fighting chance -->
    <!--[if lt IE 9]>
    <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
    <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
	<link rel="stylesheet" type="text/css" href="../static/styles.css" />
	<script>
	var _link = 'ip.html';
    </script>
</head>
<body>
<div class="main-container">
    <section id="content">
        <div class="content-wrapper">
            <section id="guide" lang="zh_cn">
                <div class="container">
                    <div class="row">
                        <div class="col-xs-12 col-sm-8 col-md-8 guide-section">
                            <div style="color:gray; word-break: break-all; font-size:12px;">原英文版地址: <a href="https://www.elastic.co/guide/en/elasticsearch/reference/7.7/ip.html" rel="nofollow" target="_blank">https://www.elastic.co/guide/en/elasticsearch/reference/7.7/ip.html</a>, 原文档版权归 www.elastic.co 所有<br/>本地英文版地址: <a href="../en/ip.html" rel="nofollow" target="_blank">../en/ip.html</a></div>
                        <!-- start body -->
                  <div class="page_header">
<strong>重要</strong>: 此版本不会发布额外的bug修复或文档更新。最新信息请参考 <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/index.html" rel="nofollow">当前版本文档</a>。
</div>
<div id="content">
<div class="breadcrumbs">
<span class="breadcrumb-link"><a href="index.html">Elasticsearch 权威指南 [7.7]</a></span>
»
<span class="breadcrumb-link"><a href="mapping.html">映射</a></span>
»
<span class="breadcrumb-link"><a href="mapping-types.html">字段数据类型</a></span>
»
<span class="breadcrumb-node">IP 数据类型</span>
</div>
<div class="navheader">
<span class="prev">
<a href="geo-shape.html">« geo_shape(地理形状)数据类型</a>
</span>
<span class="next">
<a href="parent-join.html">join(联结)数据类型 »</a>
</span>
</div>
<div class="section">
<div class="titlepage"><div><div>
<h2 class="title">
<a id="ip"></a>IP 数据类型
</h2>
</div></div></div>

<p><code class="literal">ip</code>字段可以索引/存储<a href="https://en.wikipedia.org/wiki/IPv4" class="ulink" target="_top">IPv4</a>及<a href="https://en.wikipedia.org/wiki/IPv6" class="ulink" target="_top">IPv6</a>地址。</p>
<div class="pre_wrapper lang-console">
<pre class="programlisting prettyprint lang-console">PUT my_index
{
  "mappings": {
    "properties": {
      "ip_addr": {
        "type": "ip"
      }
    }
  }
}

PUT my_index/_doc/1
{
  "ip_addr": "192.168.1.1"
}

GET my_index/_search
{
  "query": {
    "term": {
      "ip_addr": "192.168.0.0/16"
    }
  }
}</pre>
</div>
<div class="console_widget" data-snippet="snippets/655.console"></div>
<div class="note admon">
<div class="icon"></div>
<div class="admon_content">
<p>还可以使用 <a class="xref" href="range.html" title="范围数据类型" rel="nofollow">ip_range 数据类型</a> 在单个字段中存储 IP 范围。</p>
</div>
</div>
<div class="section">
<div class="titlepage"><div><div>
<h3 class="title">
<a id="ip-params"></a><code class="literal">ip</code>字段的参数
</h3>
</div></div></div>
<p><code class="literal">ip</code>字段接受以下参数：</p>
<div class="informaltable">
<table border="0" cellpadding="4px">
<colgroup>
<col>
<col>
</colgroup>
<tbody valign="top">
<tr>
<td valign="top">
<p>
<a class="xref" href="mapping-boost.html" title="boost"><code class="literal">boost</code></a>
</p>
</td>
<td valign="top">
<p>
映射字段级查询时提升，默认值为 <code class="literal">1.0</code>。
</p>
</td>
</tr>
<tr>
<td valign="top">
<p>
<a class="xref" href="doc-values.html" title="doc_values"><code class="literal">doc_values</code></a>
</p>
</td>
<td valign="top">
<p>
是否应该以列跨度(column-stride)的方式将字段存储在磁盘上，以便以后用于排序、聚合或编写脚本? 

接受<code class="literal">true</code>(默认) 或<code class="literal">false</code>。
</p>
</td>
</tr>
<tr>
<td valign="top">
<p>
<a class="xref" href="mapping-index.html" title="index"><code class="literal">index</code></a>
</p>
</td>
<td valign="top">
<p>
该字段应该是可搜索的吗？接受 <code class="literal">true</code> (默认) 和 <code class="literal">false</code>。
</p>
</td>
</tr>
<tr>
<td valign="top">
<p>
<a class="xref" href="null-value.html" title="null_value"><code class="literal">null_value</code></a>
</p>
</td>
<td valign="top">
<p>
接受替换任何显式<code class="literal">null</code>的 IPv4 值。

默认值为 <code class="literal">null</code>，这意味着该字段被视为缺失。
</p>
</td>
</tr>
<tr>
<td valign="top">
<p>
<a class="xref" href="mapping-store.html" title="store"><code class="literal">store</code></a>
</p>
</td>
<td valign="top">
<p>
字段值是否应该与<a class="xref" href="mapping-source-field.html" title="_source 字段" rel="nofollow"><code class="literal">_source</code></a>字段分开存储和检索。

接受<code class="literal">true</code>或<code class="literal">false</code> (默认)。
</p>
</td>
</tr>
</tbody>
</table>
</div>
</div>

<div class="section">
<div class="titlepage"><div><div>
<h3 class="title">
<a id="query-ip-fields"></a><code class="literal">ip</code>字段的查询
</h3>
</div></div></div>
<p>查询ip地址最常用的方法是使用<a href="https://en.wikipedia.org/wiki/Classless_Inter-Domain_Routing#CIDR_notation" class="ulink" target="_top">CIDR</a>符号：<code class="literal">[ip_address]/[prefix_length]</code>。例如：
</p>
<div class="pre_wrapper lang-console">
<pre class="programlisting prettyprint lang-console">GET my_index/_search
{
  "query": {
    "term": {
      "ip_addr": "192.168.0.0/16"
    }
  }
}</pre>
</div>
<div class="console_widget" data-snippet="snippets/656.console"></div>
<p>或者</p>
<div class="pre_wrapper lang-console">
<pre class="programlisting prettyprint lang-console">GET my_index/_search
{
  "query": {
    "term": {
      "ip_addr": "2001:db8::/48"
    }
  }
}</pre>
</div>
<div class="console_widget" data-snippet="snippets/657.console"></div>
<p>
还要注意，冒号在<a class="xref" href="query-dsl-query-string-query.html" title="Query string query"><code class="literal">query_string</code></a>查询中是特殊字符，因此 ipv6 地址需要转义。

最简单的方法是用引号将搜索的值括起来:
</p>
<div class="pre_wrapper lang-console">
<pre class="programlisting prettyprint lang-console">GET my_index/_search
{
  "query": {
    "query_string" : {
      "query": "ip_addr:\"2001:db8::/48\""
    }
  }
}</pre>
</div>
<div class="console_widget" data-snippet="snippets/658.console"></div>
</div>

</div>
<div class="navfooter">
<span class="prev">
<a href="geo-shape.html">« geo_shape(地理形状)数据类型</a>
</span>
<span class="next">
<a href="parent-join.html">join(联结)数据类型 »</a>
</span>
</div>
</div>

                  <!-- end body -->
                        </div>
                        <div class="col-xs-12 col-sm-4 col-md-4" id="right_col">
                        
                        </div>
                    </div>
                </div>
            </section>
        </div>
    </section>
</div>
<script src="../static/cn.js"></script>
</body>
</html>